
#include "RenderWidget.h"
#include "world.h"
#include "geometryutilities.h"
#include "streetmap.h"
#include <QDebug>
StreetMap *gStreetMap;

RenderWidget::RenderWidget(QWidget* parent, bool designer)
  : QWidget(parent)
{
  gStreetMap = new StreetMap("config/test_map.xml", QSize(441,381));
  resize(parent->size());
}

QPoint RenderWidget::worldToWindow(const Point &worldCoordinate)
{
  //unsigned int worldWidth = World::getInstance()->getWidth();
  //unsigned int worldHeight= World::getInstance()->getHeight();

  unsigned int worldWidth = 1000;
  unsigned int worldHeight = 1000;

  int newX = width() * ((float)worldCoordinate.x() / worldWidth);
  int newY = height() * ((float)worldCoordinate.y() / worldHeight);
  return QPoint(newX, newY);
}

void RenderWidget::paintEvent(QPaintEvent* event)
{
  QPainter painter(this);

  QPen pen;
  pen.setWidth(1);
  pen.setBrush(Qt::blue);
  painter.setPen(pen);

  // White background
  painter.fillRect(QRect(QPoint(0,0), size()), Qt::white);

  QSize carSize(6,5);
  for (int i=0; i<World::getInstance()->mVehicles.size(); i++)
  {
    QPoint winCoord = worldToWindow(World::getInstance()->mVehicles[i]->position());

    QRect carRect(winCoord,carSize);
    painter.fillRect(carRect, Qt::blue);
  }

  gStreetMap->render(painter);
}
